import getApiData, { getApisData } from "@/services/cpasRequest";




export async function getPeriodInfo(params: {
    dbname: string;
}) {
    try {
        const res = await getApiData("account.getPeriodInfo", params);
        if (res.ok) {

            const settingCol = {
                dbname: params.dbname,
                yearCnt: res.data.yearCnt,
                minData_id: res.data.minData_id
            }
            const resCol = await getApiData("account.calcReportSettingColumn", settingCol);
            if (resCol.ok) {
                return Promise.resolve({
                    data: res.data,
                    success: true,
                });
            }
            return Promise.resolve({
                data: res.data,
                success: false,
            });
        }
        return Promise.resolve({
            data: '接口account.getPeriodInfo出错了！',
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: '接口account.getPeriodInfo出错了！',
            success: false,
        });
    }
}



export async function getReportList(params: {
    dbname: string;
    isDesc: boolean
}) {
    if (!params.dbname) return Promise.resolve({
        data: 'dbname 为空',
        success: false,
    });
    const res = await getApiData("account.batchSettings.reportList", params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: res.data,
        success: false,
    });
}


// 更新 report_setting_column
export async function updateReport_setting_column(
    dbname: string,
    paramsData: any[],
) {
    paramsData.forEach((item: any) => {
        item.rowState = 'update';
        item.where = { id: item.id };
        delete item.reportDate;
        return item;
    });
    const postData = {
        apis: [
            {
                name: 'sqlApi.update',
                params: { name: `[${dbname}]..report_setting_column`, rows: paramsData }
            },
        ],
        reload: true,
        params: {}
    }
    const resApis = await getApisData(postData);
    const resUpdate = resApis.data['sqlApi.update']
    if (resApis.ok && resUpdate.ok) {
        return Promise.resolve({
            data: 'report_setting_column, 更改信息成功！',
            success: true,
        });
    }
    return Promise.resolve({
        data: resApis.data,
        success: false,
    });
}
